package com.shangting.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangting.common.entity.AttrKey;
import com.shangting.common.entity.AttrValue;
import com.shangting.web.mapper.AttrKeyMapper;
import com.shangting.web.service.AttrKeyService;
import com.shangting.web.service.AttrValueService;
import com.shangting.web.vo.attr.AttrKeyVo;
import com.shangting.web.vo.attr.AttrValueVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【attr_key(房间基本属性表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
        implements AttrKeyService {

    @Autowired
    private AttrKeyMapper attrKeyMapper;

    @Autowired
    private AttrValueService attrValueService;

    @Override
    public List<AttrKeyVo> getAttrKeyValueList() {
        ArrayList<AttrKeyVo> attrKeyVoList = new ArrayList<>();
        // 获取所有属性名
        List<AttrKey> attrKeys = attrKeyMapper.selectList(null);
        // 获取属性名的所有属性值
        for (AttrKey attrKey : attrKeys) {
            AttrKeyVo attrKeyVo = new AttrKeyVo();
            attrKeyVo.setId(attrKey.getId());
            attrKeyVo.setName(attrKey.getName());
            // 查询属性值
            QueryWrapper<AttrValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_key_id", attrKey.getId());
            List<AttrValue> list = attrValueService.list(queryWrapper);
            ArrayList<AttrValue> AttrValueList = new ArrayList<>();
            for (AttrValue attrValue : list) {
                AttrValueVo attrValueVo = new AttrValueVo();
                attrValueVo.setId(attrValue.getId());
                attrValueVo.setName(attrValue.getName());
                attrValueVo.setAttrKeyId(attrKey.getId());
                AttrValueList.add(attrValueVo);
            }
            attrKeyVo.setAttrValueList(AttrValueList);
            attrKeyVoList.add(attrKeyVo);
        }
        return attrKeyVoList;
    }
}